#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;
void drawHist(Mat &hist,int type,string name)//归一化并绘制直方图
{
    int hist_w=512;
    int hist_h=400;
    int width=2;
    Mat histIamge=Mat::zeros(hist_h,hist_w,CV_8UC3);
    normalize(hist,hist,1,0,type,-1,Mat());//type为图像类型
    for(int i=1;i<=hist.rows;i++)
    {
        rectangle(histIamge,Point(width*(i-1),hist_h-1),
        Point(width*i-1,hist_h-cvRound(hist_h*hist.at<float>(i-1))-1),
        Scalar(255,255,255),-1);
    }
    namedWindow(name,WINDOW_NORMAL);
    imshow(name,histIamge);
}
int main()
{
    system("color F0");
    Mat image=imread("/home/lwj/opencv_projeci/wendideguozhao.jpg");
    if(image.empty())
    {
        cout <<"请确认图像文件是否正确"<<endl;
        return -1;
    }
    Mat img,hist,imgp,histp;

    cvtColor(image,img,COLOR_BGR2GRAY);
    equalizeHist(img,imgp);//均衡化
    const int channels[1]={0};
    float inRanges[]={0,255};
    const float *ranges[]={inRanges};
    const int bins[1]={256};

    calcHist(&img,1,channels,Mat(),hist,1,bins,ranges);
    calcHist(&imgp,1,channels,Mat(),histp,1,bins,ranges);
    drawHist(hist,NORM_INF,"hist");
    drawHist(histp,NORM_INF,"histp");
    //比较直方图compareHist();
    imshow("均衡化",imgp);
    waitKey(0);
    return 0;
}